#!/usr/bin/python3

import os
import shutil
import subprocess
import lineage_stage4


os.makedirs("./t", exist_ok=True)
wdir = lineage_stage4.WorkDir("./t", reset=True)

s = lineage_stage4.Settings()
s.program_name = "test"
s.verbose_level = 1

ts = lineage_stage4.TargetSettings()
ts.os_version = "19.0"
ts.device_id = "sony_suzuran"

builder = lineage_stage4.Builder(s, ts, wdir)
builder.action_initialize()

print("        - Fetch...")
builder.action_fetch_source()

if os.path.exists("./t.bak"):
    shutil.rmtree("./t.bak")
subprocess.check_call(["cp", "-r", "./t", "./t.bak"])

print("        - Build...")
builder.action_build()

if os.path.exists("./t.bak2"):
    shutil.rmtree("./t.bak2")
subprocess.check_call(["cp", "-r", "./t", "./t.bak2"])

print("        - Install core applications...")
builder.action_install_core_applications()

print("        - Cleaning up...")
builder.action_cleanup()

#print("        - Finish...")
#builder.finish()

# if os.path.exists("./t.bak"):
#     shutil.rmtree("./t.bak")
# subprocess.check_call(["cp", "-r", "./t", "./t.bak"])
